/************************************************************************************
* AlbumModel.cpp                                                                        *
*                                                                                   *
* Copyright (C) 2011 Varuna L Amachi                                              *
*                                                                                   *
* This program is free software; you can redistribute it and/or modify              *
* it under the terms of the GNU General Public License as published by              *
* the Free Software Foundation; either version 2 of the License, or any             *
* later version.                                                                    *
*                                                                                   *
* This program is distributed in the hope that it will be useful, but               *
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY        *
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for       *
* more details. You should have received a copy of the GNU General Public           *
* License along with this program; if not, write to the Free Software Foundation,   *
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA                       *
*                                                                                   *
*************************************************************************************/ 

#include "AlbumModel.h"

AlbumModel::AlbumModel(QObject *parent) :
    QAbstractItemModel(parent)
{
//    m_albums = Collector::get()->albumsFromArtist(ALL_ARTISTS);
}






int AlbumModel::rowCount(const QModelIndex &parent) const {
    return m_albums.size();
}


int AlbumModel::columnCount(const QModelIndex &parent) const {
    return 5;
}


QVariant AlbumModel::data(const QModelIndex &index, int role) const {
    if(index.isValid()) {
        if(role == Qt::DisplayRole) {
            AlbumInfo *album= m_albums.at(index.row());
            switch(index.column()) {
            case 0: return album->m_artist->m_artistName;
            case 1: return album->m_albumName.isEmpty()?NOT_AVAILABLE:album->m_albumName;
            case 2: return album->m_tracks->size();
            case 3: return album->m_numPlays;
            case 4: return album->m_avgRating;
            }
        }
        else if(role == Qt::SizeHintRole) {
            return QSize(0, 18);
        }
        else if(role == Qt::FontRole) {
            return QFont("Droid Sans", 9);
        }
    }
    return QVariant();
}

///





QVariant AlbumModel::headerData(int section, Qt::Orientation, int role) const {
    if (role == Qt::TextAlignmentRole) {
        return int (Qt::AlignLeft | Qt::AlignVCenter);
    }
    else if (role == Qt::DisplayRole) {
        switch(section) {
        case 0: return tr("Artist");
        case 1: return tr("Album");
        case 2: return tr("Tracks");
        case 3: return tr("Plays");
        case 4: return tr("Rating");
        }
    }
    return QVariant();
}



QModelIndex AlbumModel::index(int row, int column, const QModelIndex &parent) const {
    return createIndex(row, column);
}


QModelIndex AlbumModel::parent(const QModelIndex &child) const {
    return QModelIndex();
}



bool AlbumModel::hasChildren(const QModelIndex &parent) const {
    if(! parent.isValid()) {
        return true;
    }
    return false;
}



void AlbumModel::setModel(QList<AlbumInfo *> model) {
    beginResetModel();
    m_albums = model;
    endResetModel();
}



bool AlbumSortFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const {
    QString album = sourceModel()->data(sourceModel()->index(source_row, 1)).toString();
    QString artist = sourceModel()->data(sourceModel()->index(source_row, 0)).toString();
    return album.contains(m_regExp, Qt::CaseInsensitive) || artist.contains(m_regExp, Qt::CaseInsensitive);
}
